<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * Author: IT宇宙人     
 * Date: 2015-09-09
 */
namespace Admin\Controller;
use Admin\Logic\GoodsLogic;
use Think\AjaxPage;
use Think\Page;
use \Qiniu\Storage;
//use \Qiniu\Auth;
//use \Qiniu\Storage\UploadManager;

class CoachController extends BaseController {
/******************************************  教练分类  ************************************************/
	private function initEditor()
    {
        $this->assign("URL_upload", U('Admin/Ueditor/imageUp',array('savepath'=>'goods'))); // 图片上传目录
        $this->assign("URL_imageUp", U('Admin/Ueditor/imageUp',array('savepath'=>'article'))); //  不知道啥图片
        $this->assign("URL_fileUp", U('Admin/Ueditor/fileUp',array('savepath'=>'article'))); // 文件上传s
        $this->assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp',array('savepath'=>'article')));  //  图片流
        $this->assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage',array('savepath'=>'article'))); // 远程图片管理
        $this->assign("URL_imageManager", U('Admin/Ueditor/imageManager',array('savepath'=>'article'))); // 图片管理        
        $this->assign("URL_getMovie", U('Admin/Ueditor/getMovie',array('savepath'=>'article'))); // 视频上传
        $this->assign("URL_Home", "");
    }    
    /**
     *  分类列表
     */
    public function categoryList(){
        if($id=I('id')){
            $info=M('coach_category')->where("id=$id")->find();
            $this->assign('info',$info);
        }
        $cat_list=M('coach_category')->where(array('status'=>1))->select();
        $this->assign('cat_list',$cat_list);
        $this->display();        
    }


    public function categoryDelete(){
        $id=M('coach_category')->where(array('id'=>I('id')))->delete();
        if($id){
            $this->success('删除成功!',U('categoryList'));
        }else{
            $this->error('删除失败，请重试!',U('categoryList'));
        }
    }
    public function categorySave(){
       $cate_name= I('cate_name');
        if(empty($cate_name))
            $this->error('名称不能为空!',U('categoryList'));
        $original_img= I('original_img');
        if(empty($original_img))
            $this->error('图片不能为空!',U('categoryList'));
        $data['name']=$cate_name;
        $data['original_img']=$original_img;
        $data['status']=1;
        $data['add_time']=time();
        if($id=I('id')){
            $data['id']=$id;
            $res=M('coach_category')->save($data);
            if($res){
                $this->success('修改成功!',U('categoryList'));
            }else{
                $this->error('修改失败，请重试!',U('categoryList'));
            }
        }else{
            $res=M('coach_category')->add($data);
            if($res){
                $this->success('添加成功!',U('categoryList'));
            }else{
                $this->error('添加失败，请重试!',U('categoryList'));
            }
        }
    }
/******************************************  预约时间  ************************************************/
    public function yyTime(){
        $time_list =M('yytime')->where(array('status'=>1))->order('id asc')->select();
        $this->assign('time_list',$time_list);
        $this->display();
    }

    public function timeSave(){
        $data['id']=I('time_id');
        $data['value']=I('time_value');
        $id=M('yytime')->save($data);
        if($id){
            $this->success('修改成功!',U('yyTime'));
        }else{
            $this->error('修改失败，请重试!',U('yyTime'));
        }
    }

/******************************************  教练  ************************************************/

    /**
     *  教练列表
     */
    public function coachList(){
        $is_open=I('is_open');
        if(!$is_open)
            $is_open=2;
        $where['is_open']=$is_open-1;
        $key_word=I('key_word');
        if(!empty($key_word)){
            $where['name'] = array('like','%'.$key_word.'%');;
        }
        $count = M('coach')->where($where)->count();
        $Page  = new Page($count,10);
        $show = $Page->show();
        $coach_list = M('coach')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();

        $cate_list=M('coach_category')->where(array('status'=>1))->select();
        $this->assign('is_open',$is_open);
        $this->assign('key_word',$key_word);
        $this->assign('cate_list',$cate_list);
        $this->assign('coach_list',$coach_list);
        $this->assign('page',$show);// 赋值分页输出

        $this->display();

    }

    /**
     * 切换教练状态
     */
    public function cut_status(){
        $data['id']=I('id');
        if(I('status')==1){
            $data['status']=0;
        }else{
            $data['status']=1;
        }

        if(I('is_open')==1){
            $data['is_open']=0;
        }else{
            $data['is_open']=1;
        }


       $res=M('coach')->save($data);
        if($res){
            $this->success('操作成功!',U('coachList'));
        }else{
            $this->error('操作失败，请重试!',U('coachList'));
        }

    }


    public function addEditCoach(){
        $id=I('id');
        if($id){//编辑
            $user =M('coach')->where(array('id'=>$id))->find();


            $user['is_open']=$user['is_open']+1;

            $this->assign('user',$user);
//            $wx_user_info =M('users')->where(array('user_id'=>$user['user_id']))->find();
//            $this->assign('wx_user_info',$wx_user_info);
        }
        //教练类别
        $cat_list=M('coach_category')->where(array('status'=>1))->select();

        $coach_grade=C('coach_grade');



        $this->assign('coach_grade',$coach_grade);
        $this->assign('cat_list',$cat_list);
        $this->initEditor(); // 编辑器
        $this->display('_coach');
    }

    public function saveCoach(){

        $data=I();
        $data['status']=1;
        $data['is_open']=I('is_open')-1;
        $data['addtime']=time();

        if($_FILES['file']['name']){
            //执行七牛图片上传
            $images_name=  $_FILES['file']['tmp_name'];
            $new_name =date('YmdHis').rand(100000,999999);
            $data['images'] = qiniu_upload_images($images_name,$new_name);
            if($data['id']){
                $coach_info =M('coach')->where(array('id'=>$data['id']))->find();
                $images =$coach_info['images'];
                $key= end(explode('/',$images));
                qiniu_del_images($key);//删除七牛云图片
            }

        }

        if($_FILES['file_banner']['name']){
            //执行七牛图片上传
            $images_name=  $_FILES['file_banner']['tmp_name'];
            $new_name =date('YmdHis').rand(100000,999999);
            $data['images_banner'] = qiniu_upload_images($images_name,$new_name);
            if($data['id']){
                $coach_info =M('coach')->where(array('id'=>$data['id']))->find();
                $images_banner =$coach_info['images_banner'];
                $key= end(explode('/',$images_banner));
                qiniu_del_images($key);//删除七牛云图片
            }
        }


        if($data['id']){//编辑
            $res =M('coach')->save($data);
        }else{//新增
            $res =M('coach')->add($data);
        }
        if($res){
            $this->success('操作成功!',U('coachList'));
        }else{
            $this->error('操作失败，请重试!',U('coachList'));
        }

    }

    public function search_user(){
        $keywords =I('keywords');
        if(!empty($keywords)){
            $where['status']=1;
            $where['nickname'] = array('like','%'.$keywords.'%');;
            $user_list =M('users')->where($where)->select();
            $this->assign('user_list',$user_list);
        }
        $this->display();
    }

    public function coachDelete(){
        $coach_info =M('coach')->where(array('id'=>I('id')))->find();
        $images =$coach_info['images'];
        $key= end(explode('/',$images));
        qiniu_del_images($key);//删除七牛云图片

        $id=M('coach')->where(array('id'=>I('id')))->delete();
        if($id){
            $this->success('删除成功!',U('coachList'));
        }else{
            $this->error('删除失败，请重试!',U('coachList'));
        }

    }

    /******************************************  教练预约管理 ************************************************/

    /**
     * 预约列表
     */

    public function coach_yy_list(){
        $date_id=I('date_id');
        $coach_id=I('coach_id');
        $status_id=I('status_id');
        if($status_id){
            $where['status'] =$status_id;
        }
        if($coach_id){
            $where['coach_id'] =$coach_id;
        }

        if($date_id==1){ //今天
            $where['yy_date'] = date("Ymd");
        }elseif($date_id==2){//7天内
            $where['yy_date'] =array('egt',date("Ymd",strtotime('-7 day')));
        }elseif($date_id==3){//本月
            $where['yy_date'] =array('egt',date('Ym01'));
        }elseif($date_id==4){//三个月
            $where['yy_date'] =array('egt',date("Ymd",strtotime('-3 month')));
        }elseif($date_id==5){//今年
            $where['yy_date'] =array('egt',date('Y0101'));
        }

        $model = M('coach_yy');
        $count = $model->where($where)->count();
        $Page  = new Page($count,10);
        $show = $Page->show();
        $order_str = "addtime DESC";
        $yy_liist = $model->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();

        $coach_list =M('coach')->where(array('status'=>1,'is_open'=>1))->select();
        $this->assign('coach_list',$coach_list);
        $this->assign('coach_id',$coach_id);
        $this->assign('status_id',$status_id);
        $this->assign('date_id',$date_id);
        $this->assign('yy_liist',$yy_liist);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();





//        $coach_list =M('coach')->where($where)->select();
//        $yy_liist=M('coach_yy')->select();
//        $this->assign('coach_list',$coach_list);
//        $this->assign('yy_liist',$yy_liist);
//        $this->display();

    }

    /**
     * 预约统计
     */
    public function coach_yy_statistics(){
        $date_id=I('date_id');
        $coach_id=I('coach_id');
        if($coach_id){
            $where['coach_id'] =$coach_id;
        }
        $where['is_open'] =1;
      $coach_list =M('coach')->where($where)->select();
        $where_1['status']=3;
        if($date_id==1){ //今天
            $where_1['yy_date'] = date("Ymd");
        }elseif($date_id==2){//7天内
            $where_1['yy_date'] =array('egt',date("Ymd",strtotime('-7 day')));
        }elseif($date_id==3){//本月
            $where_1['yy_date'] =array('egt',date('Ym01'));
        }elseif($date_id==4){//三个月
            $where_1['yy_date'] =array('egt',date("Ymd",strtotime('-3 month')));
        }elseif($date_id==5){//今年
            $where_1['yy_date'] =array('egt',date('Y0101'));
        }
      foreach($coach_list AS $k=>$v){
          $where_1['coach_id']=$v['id'];
          $v['count']=M('coach_yy')->where($where_1)->count();
          $coach_list[$k]=$v;
      }
        $coach_list = my_sort($coach_list,'count',SORT_DESC,SORT_STRING);
        $this->assign('date_id',$date_id);
        $this->assign('coach_list',$coach_list);
        $this->assign('xuhao',1);
        $this->display();

    }






}